package de.tabuma.formcreator.javascript.logik;

import de.tabuma.formcreator.entity.*;

import java.util.logging.*;

/**
  * @author $Author: $
  * @version $Revision: $
  * @since $Date: $
 */
public class SendFacade {
    private static final Logger log = Logger.getLogger(SendFacade.class.getName());

    public static String getWaitingSendWindow() {
        String message = de.tabuma.formcreator.constants.getValueFromProperties("de.tabuma.formcreator.javascript.send.waitingwindow.message");
        String str = "";
        str += "\tfunction "+JSLib.getWaitingWindow()+"() {\n";
        str += "\t\tform1 = '';\n";
        str += "\t\tform1 += '<div class=\"waitingWindow\">';\n";
        str += "\t\tform1 += '<div class=\"waitingWindow1\">';\n";
        str += "\t\tform1 += '<div class=\"waitingWindowContent\">"+message+"</div>';\n";
        str += "\t\tform1 += '<div class=\"waitingWindowImage\"></div>';\n";
        str += "\t\tform1 += '</div';\n";
        str += "\t\tform1 += '</div>';\n";
        str += "\t\t"+JSLib.getPrintIframe()+"(form1);\n";
        str += "\t}\n";
        return str;
    }

    public static String getSendMethod(String dataservlet, long projectID, String method) {
        String str = "";
        str += getWaitingSendWindow();
        str += "\tvar http = null;\n";
        str += "\tif (window.XMLHttpRequest) {\n";
        str += "\t\thttp = new XMLHttpRequest();\n";
        str += "} else if (window.ActiveXObject) {\n";
        str += "\t\thttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\n";
        str += "}\n";
        str += "\tfunction "+JSLib.getSendDataFunction()+"(str) {\n";

        str += "\t\t"+JSLib.getWaitingWindow()+"();\n";

        str += "\t\tif (http != null) {\n";
        str += "\t\t\thttp.open(\"POST\",\""+dataservlet+"\",true);\n";
        str += "\t\t\thttp.onreadystatechange = "+JSLib.getResponseDataFunction()+";\n";
        str += "\t\t\tstr = encodeURIComponent(str);\n";
        str += "\t\t\thttp.setRequestHeader(\"Content-Type\",\"application/x-www-form-urlencoded\")\n";
        str += "\t\t\thttp.send('"+de.tabuma.formcreator.constants.xmlParameter+"='+str+'&"+de.tabuma.formcreator.constants.projectParameter+"="+projectID+"&"+de.tabuma.formcreator.constants.methodParameter+"="+method+"');\n";
        str += "\t\t\t"+JSLib.getLoggingFunction()+"('xml='+str+'\\n');\n";
        str += "\t\t}\n";
        str += "\t}\n";
        return str;
    }

    public static String getResponseMethod(FormStructur responseStructur, FormStructur faultStructur) {
        String str = "";

        str += "\tfunction "+JSLib.getResponseDataFunction()+"() {\n";
        str += "\t\tif (http.readyState == 4) {\n";
        str += "\t\t\trText = decodeURIComponent(http.responseText);\n";
        str += "\t\t\t"+JSLib.getLoggingFunction()+"(rText);\n";
        //getting to know whether object is fault or "standard"
        if(faultStructur!=null) {
            str += "\t\t\tif(rText.indexOf('<"+faultStructur.getXMLName()+">')!=-1) {\n";
            str += "\t\t\t\t"+JSLib.getObjectName(faultStructur)+" = "+JSLib.getFromXmlToObjectFunction(faultStructur)+"(rText);\n";
            str += "\t\t\t\t"+JSLib.getPrintView(faultStructur)+"();\n";
            str += "\t\t\t\treturn;\n";
            str += "\t\t\t}\n";
        }
        str += "\t\t\t"+JSLib.getObjectName(responseStructur)+" = "+JSLib.getFromXmlToObjectFunction(responseStructur)+"(rText);\n";
        str += "\t\t\t"+JSLib.getPrintView(responseStructur)+"();\n";
        str += "\t\t\t\n";
        str += "\t\t}\n";
        str += "\t}\n";

        return str;
    }
}
